//package com.situ.blog.dao.impl;
//
//import com.situ.blog.dao.IBlogDao;
//import com.situ.blog.pojo.entity.Blog;
//import com.situ.blog.pojo.query.BlogQuery;
//import com.situ.blog.pojo.vo.BlogVo;
//import com.situ.blog.util.JDBCUtil;
//
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
//public class BlogDaoImpl2 implements IBlogDao {
//    @Override
//    public List<BlogVo> selectByPage(BlogQuery blogQuery) {
//        Connection connection = null;
//        PreparedStatement statement = null;
//        ResultSet resultSet = null;
//        List<Blog> list = new ArrayList<>();
//        try {
//            connection = JDBCUtil.getConnection();
//            String sql = "select * from blog where 1=1";
//            List<Object> queryList = new ArrayList<>();
//            String queryTitle = blogQuery.getTitle();
//            if (queryTitle != null && !queryTitle.equals("")) {
//                sql += " and title like ?";
//                queryList.add("%" + queryTitle + "%");
//            }
//            String queryContent = blogQuery.getContent();
//            if (queryContent != null && !queryContent.equals("")) {
//                sql += " and content like ?";
//                queryList.add("%" + queryContent + "%");
//            }
//            sql += " limit ?,?";
//            statement = connection.prepareStatement(sql);
//            for (int i = 0; i < queryList.size(); i++) {
//                statement.setObject(i + 1,queryList.get(i));
//            }
//            int offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
//            statement.setInt(queryList.size() + 1,offset);
//            statement.setInt(queryList.size() + 2, blogQuery.getLimit());
//            System.out.println(statement);
//            resultSet = statement.executeQuery();
//            while (resultSet.next()) {
//                int id = resultSet.getInt("id");
//                String title = resultSet.getString("title");
//                String image = resultSet.getString("image");
//                int clickCount = resultSet.getInt("click_count");
//                String content = resultSet.getString("content");
//                int typeId = resultSet.getInt("type_id");
//                int status = resultSet.getInt("status");
//                int deleted = resultSet.getInt("is_deleted");
//                Date createTime = resultSet.getDate("create_time");
//                Date updateTime = resultSet.getDate("update_time");
//                Blog blog = new Blog(id, title, image, clickCount, content, typeId, status, deleted, createTime,updateTime);
//                list.add(blog);
//            }
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        return list;
//    }
//
//    @Override
//    public int selectTotalCount(BlogQuery blogQuery) {
//        Connection connection = null;
//        PreparedStatement statement = null;
//        ResultSet resultSet = null;
//        int count = 0;
//        try {
//            connection = JDBCUtil.getConnection();
//            String sql = "select count(*) from blog where 1=1";
//            List<Object> queryList = new ArrayList<>();
//            String queryTitle = blogQuery.getTitle();
//            if (queryTitle != null && !queryTitle.equals("")) {
//                sql += " and title like ?";
//                queryList.add("%" + queryTitle + "%");
//            }
//            String queryContent = blogQuery.getContent();
//            if (queryContent != null && !queryContent.equals("")) {
//                sql += " and content like ?";
//                queryList.add("%" + queryTitle + "%");
//            }
//            statement = connection.prepareStatement(sql);
//            for (int i = 0; i < queryList.size(); i++) {
//                statement.setObject(i + 1,queryList.get(i));
//            }
//            System.out.println(statement);
//            resultSet = statement.executeQuery();
//            while (resultSet.next()) {
//                count = resultSet.getInt(1);
//            }
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        return count;
//    }
//
//    @Override
//    public void add(Blog blog) {
//        Connection connection = null;
//        PreparedStatement statement = null;
//        try {
//            connection = JDBCUtil.getConnection();
//            String sql = "insert into blog(title, image, content) value(?,?,?)";
//            statement = connection.prepareStatement(sql);
//            statement.setString(1, blog.getTitle());
//            statement.setString(2, blog.getImage());
//            statement.setString(3, blog.getContent());
//            System.out.println(statement);
//            int count = statement.executeUpdate();
//            System.out.println("count: " + count);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        } finally {
//            JDBCUtil.close(connection, statement, null);
//        }
//    }
//
//    @Override
//    public void deleteById(Integer id) {
//        Connection connection = null;
//        PreparedStatement statement = null;
//        try {
//            connection = JDBCUtil.getConnection();
//            String sql = "delete from blog where id=?";
//            statement = connection.prepareStatement(sql);
//            statement.setInt(1, id);
//            System.out.println(statement);
//            int count = statement.executeUpdate();
//            System.out.println("count: " + count);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        } finally {
//            JDBCUtil.close(connection, statement, null);
//        }
//
//    }
//
//    @Override
//    public Blog selectById(Integer id) {
//        Connection connection = null;
//        PreparedStatement statement = null;
//        ResultSet resultSet = null;
//        Blog blog = null;
//        try {
//            connection = JDBCUtil.getConnection();
//            String sql = "select * from blog where id=?";
//            statement = connection.prepareStatement(sql);
//            statement.setInt(1, id);
//            System.out.println(statement);
//            System.out.println(statement);
//            resultSet = statement.executeQuery();
//            while (resultSet.next()) {
//                String title = resultSet.getString("title");
//                String image = resultSet.getString("image");
//                int clickCount = resultSet.getInt("click_count");
//                String content = resultSet.getString("content");
//                int typeId = resultSet.getInt("type_id");
//                int status = resultSet.getInt("status");
//                int deleted = resultSet.getInt("is_deleted");
//                Date createTime = resultSet.getDate("create_time");
//                Date updateTime = resultSet.getDate("update_time");
//                blog = new Blog(id, title, image, clickCount, content, typeId, status, deleted, createTime,updateTime);
//            }
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        } finally {
//            JDBCUtil.close(connection, statement, resultSet);
//        }
//        return blog;
//    }
//
//    @Override
//    public void update(Blog blog) {
//        Connection connection = null;
//        PreparedStatement statement = null;
//        try {
//            connection = JDBCUtil.getConnection();
//            String sql = "update blog set title=?, content=? where id=?";
//            statement = connection.prepareStatement(sql);
//            statement.setString(1, blog.getTitle());
//            statement.setString(2, blog.getContent());
//            statement.setInt(3, blog.getId());
//            System.out.println(statement);
//            int count = statement.executeUpdate();
//            System.out.println("count: " + count);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        } finally {
//            JDBCUtil.close(connection, statement, null);
//        }
//    }
//
//    @Override
//    public void updateStatus(Integer id, Integer status) {
//        Connection connection = null;
//        PreparedStatement statement = null;
//        try {
//            connection = JDBCUtil.getConnection();
//            String sql = "update blog set status=? where id=?";
//            statement = connection.prepareStatement(sql);
//            statement.setInt(1, status);
//            statement.setInt(2, id);
//            System.out.println(statement);
//            int count = statement.executeUpdate();
//            System.out.println("count: " + count);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//    }
//}
